package dao.impl;

import bean.dto.VisitorQueryPage;
import bean.pojo.Room;
import bean.pojo.Visitor;
import commons.dbHelper.DBHelper;
import commons.xlhUtils.MyFormatUtils;
import dao.VisitorDao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * dao.impl
 * User: 坚持
 * Date: 2024/12/18 11:30
 * motto: 树欲静而风不止
 * Description:
 * Version: V1.0
 */
public class VisitorDaoImpl implements VisitorDao {
    /**
     * 访客的添加
     * @param visitor
     * @return
     */
    @Override
    public Integer add(Visitor visitor) {
        String sql ="insert visitor  (visitor_name,visitor_sex,id_card_number," +
                "visitor_phone,remarks,visit_time) values (?,?,?,?,?,?)";
        DBHelper dbHelper = new DBHelper();
        return dbHelper.update(sql,visitor.getVisitorName(),visitor.getVisitorSex(),
                visitor.getIdCardNumber(),visitor.getVisitorPhone(),visitor.getRemarks(),
                visitor.getVisitTime());
    }

    /**
     * 根据ID删除访客记录
     * @param visitorId
     * @return
     */
    @Override
    public Integer deleteById(Integer visitorId) {
        String sql = " delete from visitor where visitor_id = ? ";
        DBHelper dbHelper = new DBHelper();
        return dbHelper.update(sql,visitorId);
        
    }

    /**
     * 修改访客信息
     * @param visitor
     * @return
     */
    @Override
    public Integer update(Visitor visitor) {
        String sql ="update visitor set visitor_name=?,visitor_sex=?,id_card_number=?,"+
                "visitor_phone=?,remarks=?,visit_time=? where visitor_id=?";
        DBHelper dbHelper = new DBHelper();
        return dbHelper.update(sql,visitor.getVisitorName(),visitor.getVisitorSex(),
                visitor.getIdCardNumber(),visitor.getVisitorPhone(),visitor.getRemarks(),
                visitor.getVisitTime(),visitor.getVisitorId());
    }

    /**
     * 查询总的数据条数
     * @param visitorQueryPage
     * @return
     */
    @Override
    public Long findByTotal(VisitorQueryPage visitorQueryPage) {

        StringBuffer sql = new StringBuffer("select visitor_id from visitor where 1=1");
        List<Object>  parameters =new ArrayList<>();
        if (visitorQueryPage.getVisitorName() != null) {
            sql.append(" and visitor_name like concat ('%',?,'%') ");
            parameters.add(visitorQueryPage.getVisitorName());
        }
        if (visitorQueryPage.getVisitorPhone() != null) {
            sql.append(" and visitor_phone like concat ('%',?,'%') ");
            parameters.add(visitorQueryPage.getVisitorPhone());
        }

        if (visitorQueryPage.getVisitTime() !=null) {
            sql.append(" and visit_time like concat ('%',?,'%') ");
            parameters.add(visitorQueryPage.getVisitTime());
        }

        System.out.println("sql:" + sql);
        System.out.println("参数:"+parameters);
        DBHelper dbHelper = new DBHelper();
        List<Visitor> query = dbHelper.getBeanList(Visitor.class,sql.toString(),parameters.toArray());
        if (query!=null) {
            return (long) query.size();
        }
        return 0L;
    }

    /**
     * 根据姓名、电话、时间查询访客信息
     * @param visitorQueryPage
     * @return
     */
    @Override
    public List<Visitor> visitorFindBy(VisitorQueryPage visitorQueryPage) {
        StringBuffer sql = new StringBuffer("select visitor_id,visitor_name,visitor_sex,id_card_number,"+
                "visitor_phone,remarks,visit_time from visitor where 1=1");
        List<Object>  parameters =new ArrayList<>();
        if (visitorQueryPage.getVisitorName() != null) {
            sql.append(" and visitor_name like concat ('%',?,'%') ");
            parameters.add(visitorQueryPage.getVisitorName());
        }
        if (visitorQueryPage.getVisitorPhone() != null) {
            sql.append(" and visitor_phone like concat ('%',?,'%') ");
            parameters.add(visitorQueryPage.getVisitorPhone());
        }
        if (visitorQueryPage.getVisitTime() !=null) {
            sql.append(" and visit_time like concat ('%',?,'%') ");
            parameters.add(visitorQueryPage.getVisitTime());
        }
        sql.append(" order by visitor_id asc ");
        sql.append(" limit " + (visitorQueryPage.getPage() - 1) * visitorQueryPage.getLimit() + "," + visitorQueryPage.getLimit());
        System.out.println("sql:" + sql);
        System.out.println("参数:"+parameters);
        DBHelper dbHelper = new DBHelper();
        return dbHelper.getBeanList(Visitor.class,sql.toString(),parameters.toArray());
    }
}
